package top.huhuiyu.springboot2.study.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import top.huhuiyu.springboot2.study.mapper.UtilMapper;
import top.huhuiyu.springboot2.study.util.IpUtils;

/**
 * @author 胡辉煜
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
  private final Environment environment;
  private final UtilMapper utilMapper;

  @Override
  public void onApplicationEvent(ApplicationReadyEvent event) {
    log.debug("应用程序启动");
    // 通过环境信息获取应用程序端口
    Integer port = environment.getProperty("server.port", Integer.class);
    log.debug("应用程序路径：http://{}:{}", IpUtils.getIpAddress(), port);
    log.debug("应用程序路径：http://127.0.0.1:{}", port);
    // 获取数据库当前时间
    log.debug("数据库当前时间：{}", utilMapper.queryNow());
  }
}
